<?php

namespace Admin\Controller;

use Admin\Model\UserModel;
use Think\Controller;

//v2-审核管理
class CheckManagerController extends CommenController
{

    /***
     * 待审核借款
     */
      public function wait_check_borrow(){

          $application_time = I('application_time');//申请时间；
          $search = I('key');//搜索
          $show_num=I('show_num');
          $shop_identifier = $_SESSION['shop_identifier'];
          //分割时间
          $apply_arr = explode(' - ', $application_time);
          $search_log='【审核管理-待审核借款】搜索：';
          $is_search=0;
          $where = [];
          if (!empty($application_time)) {
              $a_start = strtotime($apply_arr[0]);
              $a_end = strtotime($apply_arr[1]);
              $where['d.ctime'] = array('between', array($a_start, $a_end));
              $search_log=$search_log.' 申请时间为 '.$application_time;
              $is_search=1;
          }

          $db_user = M('user', '', 'XYC');
          //搜索字段
          $search_arr = array('key' => $search, 'application_time' => $application_time,'show_num'=>$show_num);

          if (!empty($search)) {
              $where['a.username|a.mobile|a.identity_num'] = array('like', '%' . $search . '%');
              $search_log=$search_log.' 关键字为 '.$search;
              $is_search=1;
          }

          //日志记录
          if($is_search==1){
              add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],$search_log,2);
          }

          $where['a.shop_identifier'] = $shop_identifier;
//        $where['a.cert_status'] = 3;
          //权限
          $role=M('shopkeeper_role','','XYC')->where(['shop_identifier'=>$_SESSION['shop_identifier'],'sid'=>$_SESSION['id']])->getfield('rid',true);

          $w=[];
          $flag=0;
          if(in_array('3',$role)){//催收
              $w['b.collector']=$_SESSION['id'];
              $flag=1;
          }

          if(in_array('4',$role)){//客服
              $w['a.chat_id']=$_SESSION['id'];
              $flag=1;
          }

          if(in_array('11',$role)){//审核员
              $w['b.approver']=$_SESSION['id'];
              $flag=1;
          }

          if(in_array('6',$role)){//渠道账号
              $w['a.channel']=$_SESSION['chan_id'];
              $flag=1;
          }

          if($flag==1){
              $w['_logic']='OR';
              $where['_complex']=$w;
          }

          $tol = $db_user->alias('a')->join('left join user_login b on b.user_id=a.id ')->join('join vz_loan d on d.user_id=a.id and d.status=0')->where($where)->COUNT();
          $row = '10';
          if(!empty($show_num)){
              $row=$show_num;
          }
          $page = new PageController($tol, $row);
          $fpage = $page->fpage();
          $field = 'a.id as user_id,a.mobile,a.username,d.ctime,a.quota,d.loan_amount,d.id as loan_id,d.order_number,c.lender_id';
          $data = $db_user->where($where)->join('a left join user_login b on b.user_id=a.id ')->join(' left join channel c on c.chan_id=a.channel')->join('join vz_loan d on d.user_id=a.id and d.status=0')->field($field)->limit($page->listfirst, $page->listRows)->order('d.id desc')->select();

          //借款人
          $lender=M('lender','','XYC')->where(['id'=>['in',array_column($data,'lender_id')]])->getfield('id,name',true);


          foreach ($data as $k=>$v){

              $data[$k]['lender'] = $lender[$v['lender_id']];

              $data[$k]['ctime']=date('Y-m-d H:i:s',$v['ctime']);

              //脱敏
              if($_SESSION['is_hide']=='2'){
                  $data[$k]['mobile']=set_privacy($v['mobile'],1);
                  $data[$k]['identity_num']=set_privacy($v['identity_num'],2);
              }
          }
          $this->assign(array(
              'data' => $data,
              'fpage' => $fpage,
              'search' => $search_arr,
              'tol' => $tol
          ));
          $this->display();
      }

      /**
       * 借款审核
       */
      public function check_loan(){
          $loan_id=I('loan_id');
          $result=I('result');
          $loan=M('vz_loan','','XYC')->where(['id'=>$loan_id])->field('status,user_id,order_number,loan_amount')->find();
          if($loan['status']!='0'){
              $this->ajaxReturn(['code'=>'1002','msg'=>'该订单非审核状态']);
              die;
          }
          $update=M('vz_loan','','XYC')->where(['id'=>$loan_id])->setfield('status',$result);
          if($update){
              ///添加记录
              if($result=='4'){
                  M('user','','XYC')->where(['id'=>$loan['user_id']])->setInc('balance',$loan['loan_amount']);
              }
              $arr=['reason'=>'','user_id'=>$loan['user_id'],'order_number'=>$loan['order_number'],'ctime'=>time(),'operator'=>$_SESSION['account'],'shop_identifier'=>$_SESSION['shop_identifier'],'type'=>$result];
              M('check_borrow_record','','XYC')->add($arr);
              $this->ajaxReturn(['code'=>'1001','msg'=>'操作成功']);
              die;
          }
          $this->ajaxReturn(['code'=>'1002','msg'=>'操作失败']);
          die;
      }


    /**
     * 今日审核
     */
      public function today_check(){

          $shop_identifier = $_SESSION['shop_identifier'];
          //搜索字段
          $search_arr = array('key' => I('key'), 'start' => I('start'), 'end' => I('end'));
          $where = [];
          $search_log='【审核管理-今日审核】搜索：';
          $is_search=0;
          if (!empty(I('start')) || !empty(I('end'))) {
              $start_time = strtotime(I('start'));
              $end_time = strtotime(I('end'));
              if (empty(I('end'))) {
                  $where['a.ctime'] = array('egt', $start_time);
              } else if (empty(I('start'))) {
                  $where['a.ctime'] = array('elt', $end_time);
              } else {
                  $where['a.ctime'] = array('between', array($start_time, $end_time));
              }
              $is_search=1;
              $search_log=$search_log.' 申请时间为 '.I('start').'--'.I('end');
          }
          if (!empty(I('key'))) {
              $search=I('key');
              $is_search=1;
              $where['b.username|b.mobile|b.identity_num'] = array('like', $search . '%');
              $search_log=$search_log.' 关键字为 '.$search;
          }
          if($is_search==1){
              add_control_log($_SESSION['shop_identifier'],$_SESSION['name'],$search_log,2);
          }
          $where['a.shop_identifier'] = $shop_identifier;

          $s_time=strtotime(date('Y-m-d',time()));
          $where['a.ctime']=['between',[$s_time,time()]];
          $tol = M('check_borrow_record','','XYC')->join('a join user b on b.id=a.user_id')->where($where)->COUNT();

          $row = '10';
          if(!empty($show_num)){
              $row=$show_num;
          }
          $page = new PageController($tol, $row);
          $fpage = $page->fpage();
          $field = 'b.mobile,b.username,b.id as user_id,a.order_number,b.quota,a.operator,a.type,c.lender_id';
          $data = M('check_borrow_record','','XYC')->join('a join user b on b.id=a.user_id')->join('left join channel c on c.chan_id=b.channel')->where($where)->field($field)->limit($page->listfirst, $page->listRows)->order('a.id desc')->select();
          $loans=M('vz_loan','','XYC')->where(['order_number'=>['in',array_column($data,'order_number')]])->getfield('order_number,loan_amount,ctime',true);
          $operator=M('shopkeeper','','XYC')->where(['account'=>['in',array_column($data,'operator')],'shop_identifier'=>$shop_identifier])->getfield('account,name,id',true);
          //借款人
          $lender=M('lender','','XYC')->where(['id'=>['in',array_column($data,'lender_id')]])->getfield('id,name',true);

          foreach ($data as $k=>$v){

              if($v['type']=='4'){
                  $data[$k]['result']='拒绝';
              }else    if($v['type']=='5'){
                  $data[$k]['result']='通过';
              }


              $data[$k]['loan_amount']=$loans[$v['order_number']]['loan_amount'];
              $data[$k]['apply_time']=date('Y-m-d H:i:s',$loans[$v['order_number']]['ctime']) ;
              $data[$k]['operator']=$operator[$v['operator']]['name'];
              $data[$k]['lender']=$lender[$v['lender_id']];

              //脱敏
              if($_SESSION['is_hide']=='2'){
                  $data[$k]['mobile']=set_privacy($v['mobile'],1);
                  $data[$k]['identity_num']=set_privacy($v['identity_num'],2);
              }
          }

          $this->assign(array(
              'data' => $data,
              'fpage' => $fpage,
              'search' => $search_arr,
              'tol' => $tol
          ));
          $this->display();

      }
}